<html>
  <head>
    <script src="https://apprtc.appspot.com/_ah/channel/jsapi"></script>
  </head>
  <!--
  Helper HTML that redirects Google AppEngine's Channel API to a JS object named
  |androidMessageHandler|, which is expected to be injected into the WebView
  rendering this page by an Android app's class such as AppRTCClient.
  -->
  <body onbeforeunload="closeSocket()" onload="openSocket()">
    <script type="text/javascript">
      var token = androidMessageHandler.getToken();
      if (!token)
        throw "Missing/malformed token parameter: [" + token + "]";

      var channel = null;
      var socket = null;

      function openSocket() {
        channel = new goog.appengine.Channel(token);
        socket = channel.open({
          'onopen': function() { androidMessageHandler.onOpen(); },
          'onmessage': function(msg) { androidMessageHandler.onMessage(msg.data); },
          'onclose': function() { androidMessageHandler.onClose(); },
          'onerror': function(err) { androidMessageHandler.onError(err.code, err.description); }
        });
      }

      function closeSocket() {
        socket.close();
      }
    </script>
  </body>
</html>
